package com.yichenyang.ordersystem.controller;

import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStreamReader;


@Controller
public class PageController {
    private static ClassLoader classLoader = ClassUtils.getDefaultClassLoader();

    @RequestMapping("/")
    public void root(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/index.html").forward(request, response);
    }

    @RequestMapping("pages/**")
    public String page(HttpServletRequest request, HttpServletResponse response) throws IOException {
        InputStreamReader streamReader = null;
        try {
            String servletPath = request.getServletPath();
            String filePath = servletPath;
            if (filePath.endsWith(".html")) {
                return filePath.substring(0,filePath.length()-5);
            }
        } finally {
            if (streamReader != null) streamReader.close();
        }
        return "index";
    }
}
